@extends('layout')
@section('log-viewer::content')
    <div class="container-fluid">
        <div class="row">
            <div class="col-lg-12">
                <div class="page-header clearfix">
                    <h1 class="pull-left">日志信息</h1>
                    <div class="pull-right header-btns">
                        <button onclick="location.reload()" class="btn btn-sm btn-primary" style="margin-right: 3px;">刷新页面</button>
                        <a href="{{route('log-viewer-download')}}?file={{$service->getLogName()}}" class="btn btn-sm btn-success" style="margin-right: 3px;">下载</a>
                        <a href="{{route('log-viewer-delete')}}?file={{ $service->getLogName()}}" data-url="{{route('log-viewer-delete')}}?file={{ $service->getLogName()}}" class="btn btn-sm btn-danger delete-btn">删除</a>
                    </div>
                </div>
            </div>
        </div>

        <div class="row">
            <div class="col-lg-12">
                <div class="panel panel-default">
                    <div class="panel-body">
                        <div class="row">
                            <div class="col-md-6">
                                <p><strong>日志路径：</strong>{{ $service->getLogName() }}</p>
                                <p><strong>日志条数：</strong>{{ count($service->getLogContents()) }}</p>
                            </div>
                            <div class="col-md-6">
                                <p><strong>日志打印：</strong>
                                    <a href="{{route('log-viewer-ddcontent')}}?file={{ $service->getLogName() }}&is_y=0" class="btn btn-xs btn-info">打印</a>
                                </p>
                                <p><strong>原日志打印：</strong>
                                    <a href="{{route('log-viewer-ddcontent')}}?file={{ $service->getLogName() }}&is_y=1" class="btn btn-xs btn-info">打印</a>
                                </p>
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-md-12">
                                <p><strong>日志大小：</strong>{{ $service->getLogSize() }}</p>
                                <p><strong>最近修改时间：</strong>{{ $service->getLogModified() }}</p>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <div class="row">
            <div class="col-lg-12">
                <div class="panel panel-default">
                    <div class="panel-heading">
                        <h3 class="panel-title">日志详情</h3>
                    </div>
                    <div class="panel-body">
                        <div class="table-responsive">
                            <table width="100%" class="table table-striped table-bordered table-hover" id="dataTables-log-viewer">
                                <thead>
                                <tr>
                                    <th width="10%">级别</th>
                                    <th width="10%">环境</th>
                                    <th width="20%">日期</th>
                                    <th width="60%">内容</th>
                                </tr>
                                </thead>
                                <tbody>
                                @foreach ($service->getLogContents() as $content)
                                    <tr class="odd gradeX">
                                        <td class="{{ $service->getLevelColor($content['level']) }}">
                                            <i class="fa fa-fw {{ $service->getLevelIcon($content['level']) }}"></i> {{ $content['level'] }}
                                        </td>
                                        <td>{{ $content['env'] }}</td>
                                        <td>{{ $content['datetime'] }}</td>
                                        <td>{{ $content['message'] }}</td>
                                    </tr>
                                @endforeach
                                </tbody>
                            </table>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
@endsection

@section("log-viewer::script")
    @parent
    <script>
        $(document).ready(function() {
            // 初始化MetisMenu
            if (typeof $.fn.metisMenu !== 'undefined' && $('#side-menu').length) {
                $('#side-menu').metisMenu();
            }

            // 初始化DataTable
            $('#dataTables-log-viewer').DataTable({
                "responsive": true,
                "processing": true,
                "ordering": true,
                "order": [],
                "fixedHeader": {{ config('logviewer.fix_header', false) ? 'true' : 'false' }},
                "lengthChange": true,
                "info": true,
                "pagingType": "full_numbers",
                "lengthMenu": [{{ config('logviewer.page_size_menu', '[10, 25, 50, 100]') }}],
                "pageLength": {{ config('logviewer.default_page_size', 25) }},
                "language": {
                    "paginate": {
                        "first": "首页",
                        "last": "末页",
                        "next": "下一页",
                        "previous": "上一页"
                    },
                    "search": "搜索:",
                    "lengthMenu": "显示 _MENU_ 条记录",
                    "info": "显示第 _START_ 到 _END_ 条记录，共 _TOTAL_ 条",
                    "infoEmpty": "没有记录",
                    "infoFiltered": "(从 _MAX_ 条记录中过滤)"
                },
                "autoWidth": false,
                "columns": [
                    { "width": "10%" },
                    { "width": "10%" },
                    { "width": "20%" },
                    { "width": "60%" }
                ]
            });

            // 删除按钮点击事件
            $(".delete-btn").click(function (e) {
                e.preventDefault();
                var thisObj = $(this);
                var deleteUrl = thisObj.data('url');
                
                if (confirm("你确定删除该日志文件吗？")) {
                    $.get(deleteUrl, function (result) {
                        if(result.status == 'success'){
                            window.location.href = result.redirect;
                        } else {
                            alert(result.message);
                            window.location.reload();
                        }
                    }).fail(function() {
                        alert('删除请求失败，请重试');
                    });
                }
            });
        });
    </script>
@endsection
